﻿using System.Diagnostics;
using System;
using Utils;
using HWT;
using System.Net;

//ThreadPool.SetMaxThreads(600, 600);
//ThreadPool.SetMinThreads(550, 550);
int num = 0;

_ = Task.Factory.StartNew(() =>
{
    Stopwatch sw = Stopwatch.StartNew();
    while (true)
    {
        Thread.Sleep(5000);
        double speed = num / sw.Elapsed.TotalSeconds;
        Console.WriteLine($"10000个循环干活总速度={speed:#### ####.0} 次/秒");
    }
}, TaskCreationOptions.LongRunning);

WheelTimer timer = new WheelTimer(15);
HashedWheelTimer timer2 = new HashedWheelTimer(TimeSpan.FromMilliseconds(15), 500, -1);

for (int i = 0; i < 500; i++)
{
    ReadPLC2(i);
}

async void ReadPLC(int plcIndex)
{
    while (true)
    {
        //await Task.Run(() =>
        //{
        // todo: 读取PLC
        string url = "http://localhost:5028/Test/TestGet";
        var request = new HttpRequestMessage(HttpMethod.Get, url);
        request.Headers.Add("token", "1");
        HttpClient httpClient = HttpClientFactory.GetClient();
        string result = await (await httpClient.SendAsync(request)).Content.ReadAsStringAsync();
        Interlocked.Increment(ref num);
        // Console.WriteLine($"读取PLC完成，plcIndex={plcIndex}");
        //});

        await timer.Delay();
        //await Task.Delay(15);
    }
}

async void ReadPLC2(int plcIndex)
{
    while (true)
    {
        await Task.Run(() =>
        {
            // todo: 读取PLC
            string url = "http://localhost:5028/Test/TestGet";
            WebHeaderCollection headers = new WebHeaderCollection();
            headers.Add("token", "1");
            string result = HttpUtil.HttpGet(url, null, headers);
            Interlocked.Increment(ref num);
            // Console.WriteLine($"读取PLC完成，plcIndex={plcIndex}");
        });

        //await timer.Delay();
        await Task.Delay(15);
    }
}

Console.WriteLine("开始");
Console.ReadLine();
